package com.itboye.jinpuyiyuan.activity.me;

import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.jinpuyiyuan.R;
import com.itboye.jinpuyiyuan.base.BaseActivity;
import com.itboye.jinpuyiyuan.utils.GridItem;
import com.itboye.jinpuyiyuan.utils.ImageScanner;
import com.itboye.jinpuyiyuan.utils.StickyGridAdapter;
import com.itboye.jinpuyiyuan.utils.YMComparator;
import com.itboye.jinpuyiyuan.widget.loadingutil.MAlert;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.TimeZone;

public class HeadPresActivity extends BaseActivity {
    private ProgressDialog mProgressDialog;
    private ImageScanner mScanner;
    private GridView mGridView;
    private List<GridItem> mGirdList = new ArrayList<GridItem>();
    private static int section = 1;
    private Map<String, Integer> sectionMap = new HashMap<String, Integer>();
    private TextView tvTitle;
    private ImageView back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_head_pre);
        tvTitle.setText("图片选择");
        mGridView = (GridView) findViewById(R.id.asset_grid);
        mScanner = new ImageScanner(this);


        mScanner.scanImages(new ImageScanner.ScanCompleteCallBack() {
            {
                mProgressDialog = ProgressDialog.show(HeadPresActivity.this, null, "正在加载...");
            }

            @Override
            public void scanComplete(Cursor cursor) {
                // 关闭进度条
                mProgressDialog.dismiss();

                while (cursor.moveToNext()) {
                    // 获取图片的路径
                    String path = cursor.getString(cursor
                            .getColumnIndex(MediaStore.Images.Media.DATA));
                    long times = cursor.getLong(cursor
                            .getColumnIndex(MediaStore.Images.Media.DATE_ADDED));

                    GridItem mGridItem = new GridItem(path, paserTimeToYM(times));
                    mGirdList.add(mGridItem);

                }
                cursor.close();
                Collections.sort(mGirdList, new YMComparator());

                for (ListIterator<GridItem> it = mGirdList.listIterator(); it.hasNext(); ) {
                    GridItem mGridItem = it.next();
                    String ym = mGridItem.getTime();
                    if (!sectionMap.containsKey(ym)) {
                        mGridItem.setSection(section);
                        sectionMap.put(ym, section);
                        section++;
                    } else {
                        mGridItem.setSection(sectionMap.get(ym));
                    }
                }

                mGridView.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1,
                                            int position, long arg3) {
                        // TODO Auto-generated method stub
                        System.out.println("mGirdList" + mGirdList.get(position).getPath());
                        if (mGirdList.get(position).getPath().equals("") || mGirdList.get(position).getPath() == null) {
                            MAlert.alert("您还未选择图片");
                            return;
                        } else {
                            Intent intent = new Intent();
                            Log.d("localSelectPathImas", mGirdList.get(position).getPath());
                            intent.putExtra("path", mGirdList.get(position).getPath());
                            setResult(PersonInfoActivity.PHOTO_REQUEST_GALLERY, intent);
                            finish();
                        }
                    }
                });
                mGridView.setAdapter(new StickyGridAdapter(HeadPresActivity.this, mGirdList, mGridView));

            }
        });

    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }

    public static String paserTimeToYM(long time) {
        System.setProperty("user.timezone", "Asia/Shanghai");
        TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
        TimeZone.setDefault(tz);
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
        return format.format(new Date(time * 1000L));
    }

}
